package com.adayo.ble;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;

import com.adayo.ble.bean.BLEDevice;
import com.adayo.ble.callback.BaseResultCallback;
import com.adayo.ble.callback.BluetoothClient;
import com.adayo.ble.callback.OnSearchListener;
import com.adayo.ble.utils.JLogKit;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;


public class BluetoothClientBLEV2Adapter implements BluetoothClient {
	private BluetoothLeInitialization mClient;

	public BluetoothClientBLEV2Adapter(BluetoothLeInitialization client) {
		mClient = client;
	}

	@Override
	public void search(final int millis, final boolean cancel, final OnSearchListener listener) {
		BluetoothLeSearcher searcher = mClient.getBluetoothSearcher();

		if (searcher.isScanning() && !cancel) {
			listener.onError("is searching now");
			return;
		}

		if (searcher.isScanning()) {
			stopSearch();
		}
		listener.onStart();
		mClient.getBluetoothSearcher()
				.scanLeDevice(millis, new BluetoothLeSearcher.OnScanCallback() {
					private Set<BLEDevice> devices = new HashSet<>();

					@Override
					public void onLeScan(BluetoothDevice device,
										 int rssi,
										 byte[] scanRecord) {

						BLEDevice bleDevice = new BLEDevice();
						bleDevice.setDeviceName(device.getName());
						bleDevice.setMac(device.getAddress());
						bleDevice.setRssi(rssi);

						if (devices.contains(bleDevice)) {
							return;
						}
						listener.onFindDevice(bleDevice);
						devices.add(bleDevice);
					}

					@Override
					public void onComplete() {
						listener.onComplete();
					}

					@Override
					public void onError(String msg) {
						listener.onError(msg);
					}
				});
	}

	@Override
	public void stopSearch() {
		mClient.getBluetoothSearcher().stopScanLeDevice();
	}

	@Override
	public void connect(final String mac) {
		BluetoothLeConnector connector = mClient.getBluetoothLeConnector(mac);

		connector.connect(new BluetoothLeConnector.OnConnectListener() {
			@Override
			public void onConnect() {
				JLogKit.getInstance().i("onConnect");
			}

			@Override
			public void onDisconnect() {
				JLogKit.getInstance().i("onDisconnect");
			}

			@Override
			public void onServiceDiscover() {
				JLogKit.getInstance().i("onServiceDiscover");
			}

			@Override
			public void onError(String msg) {
				JLogKit.getInstance().i("onError = " + msg);
			}
		});
	}

	@Override
	public void disconnect(String mac) {
		mClient.getBluetoothLeConnector(mac).disconnect();
	}

	@Override
	public void write(String mac, byte[] values){
		BluetoothLeConnector connector = mClient.getBluetoothLeConnector(mac);
		connector.sendCommand(values);

		final BluetoothLeConnector.OnDataAvailableListener onConnectListener
				= connector.getOnDataAvailableListener();

		if(onConnectListener == null){
			JLogKit.getInstance().i("onConnectListener == null");
		}
		connector.setOnDataAvailableListener(new BluetoothLeConnector.OnDataAvailableListener() {
			@Override
			public void onCharacteristicRead(byte[] values, int status) {
				if(onConnectListener != null){
					onConnectListener.onCharacteristicRead(values, status);
				}
			}

			@Override
			public void onCharacteristicChange(UUID characteristic, byte[] values) {
				JLogKit.getInstance().i("onCharacteristicChange size = " + values.length);
				if(onConnectListener != null){
					onConnectListener.onCharacteristicChange(characteristic, values);
				}
			}

			@Override
			public void onCharacteristicWrite(UUID characteristic, int status) {
				JLogKit.getInstance().i("onCharacteristicWrite status = " + status);
			}

			@Override
			public void onDescriptorWrite(UUID descriptor, int status) {
				if(onConnectListener != null){
					onConnectListener.onDescriptorWrite(descriptor, status);
				}
			}

			@Override
			public void onError(String msg) {
				JLogKit.getInstance().i("onError = " + msg);
			}
		});
	}

	@Override
	public void clean(String mac) {
		mClient.cleanConnector(mac);
	}

	@Override
	public void cleanAll() {
		mClient.cleanAllConnector();
	}

	@Override
	public void openBluetooth() {
		mClient.initialize();
	}

	@Override
	public void closeBluetooth() {

	}
}
